package com.health.mapp.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.health.db.model.VersionControl;
import com.health.db.model.VersionControlExt;
import com.health.db.service.sm.IVersionControlService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.VersionUpdateReq;
import com.health.mapp.bean.VersionUpdateResp;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;
import com.health.mapp.utils.StringUtils;

public class VersionUpdateImpl implements ActionInterface {

	private static Logger logger = Logger.getLogger(VersionUpdateImpl.class);
	
	private IVersionControlService versionControlService;

	@Override
	public Response execute(Header header, Object reqBean) throws SQLException {

		// do it bobo

		VersionUpdateReq req = (VersionUpdateReq) reqBean;
		VersionUpdateResp resp = new VersionUpdateResp();

		// 0: android; 1: ios
		int os = req.getOs() == 1 ? 1 : 0;

		String pattern = String.valueOf(os);// 操作系统标识
		int flag=req.getFlag();//客户端标识 0:用户版 1：医生版
		String version = req.getCurrentVersion();// 版本号
		if(StringUtils.isBlank(version))
		{
			resp.setErrorCode(ResultCode.VERSION_UPDATE_VERSION_ERROR);
			resp.setErrorMsg(ResultCode.VERSION_UPDATE_VERSION_ERROR_NOTE);
			
			return resp;
		}

		VersionControlExt queryCondition = new VersionControlExt();
		queryCondition.setPattern(pattern);
		queryCondition.setVersion(version);

		List<VersionControl> versionLastesList = versionControlService.selectLastestVersion(pattern,flag);
		
		if (null != versionLastesList && versionLastesList.size() == 1) {
			VersionControl versionLastesControl  = versionLastesList.get(0);
			resp.setVersion(versionLastesControl.getVersion());
			resp.setAppPath(versionLastesControl.getAppPath());
			resp.setReason(versionLastesControl.getReason());
			//resp.setUpdateTag(versionControl.getUpdateTag());
			resp.setLoadPage1(versionLastesControl.getLoadPage1());
			resp.setLoadPage2(versionLastesControl.getLoadPage2());
			resp.setLoadPage3(versionLastesControl.getLoadPage3());
			resp.setLoadPage4(versionLastesControl.getLoadPage4());
		}else {
			resp.setErrorCode(ResultCode.VERSION_UPDATE_VERSION_ERROR);
			resp.setErrorMsg(ResultCode.VERSION_UPDATE_NO_VERSION_ERROR_NOTE);
		}

		if (!resp.getVersion().equals(version)) {
			
			List<VersionControl> versionList = versionControlService.selestVersionControl(pattern,flag,version);
			if (null != versionList && versionList.size() == 1) {
				VersionControl versionControl  = versionLastesList.get(0);
				versionControl = versionList.get(0);
				resp.setUpdateTag(versionControl.getUpdateTag());
			}else {
				resp.setUpdateTag("2");
			}
		}else{
			resp.setUpdateTag("0");
		}
		return resp;
	}

	public IVersionControlService getVersionControlService() {
		return versionControlService;
	}

	public void setVersionControlService(
			IVersionControlService versionControlService) {
		this.versionControlService = versionControlService;
	}
}
